package com.lefevre.cms.web.action.follow;

import com.lefevre.cms.bean.follow.Follow;
import com.lefevre.cms.bean.message.Remind;
import com.lefevre.cms.bean.topic.Comment;
import com.lefevre.cms.bean.topic.Reply;
import com.lefevre.cms.bean.topic.Topic;
import com.lefevre.cms.bean.user.User;
import com.lefevre.cms.service.follow.FollowService;
import com.lefevre.cms.service.message.RemindService;
import com.lefevre.cms.service.topic.CommentService;
import com.lefevre.cms.service.topic.TopicService;
import com.lefevre.cms.utils.UUIDUtil;
import com.lefevre.cms.utils.Verification;
import com.lefevre.cms.web.action.message.RemindManage;
import com.lefevre.cms.web.action.user.UserManage;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * 关注管理
 */
@Component("followManage")
public class FollowManage {

    @Resource
    FollowService followService;
    @Resource
    UserManage userManage;
    @Resource
    RemindService remindService;
    @Resource
    TopicService topicService;
    @Resource
    RemindManage remindManage;
    @Resource
    CommentService commentService;

    @Lazy
    @Resource
    FollowManage followManage;

    /**
     * 取得关注Id的用户Id(后N位)
     *
     * @param followId 关注Id
     * @return
     */
    public int getFollowAfterId(String followId) {
        String[] idGroup = followId.split("-");
        long userId = Long.parseLong(idGroup[1]);
        String afterUserId = String.format("%04d", userId % 10000);
        return Integer.parseInt(afterUserId);
    }

    /**
     * 生成关注Id
     * 关注Id由(对方用户Id-关注的用户Id)组成
     *
     * @param userId       用户Id
     * @param friendUserId 对方用户Id
     * @return
     */
    public String createFollowId(Long userId, Long friendUserId) {
        return String.valueOf(friendUserId) + "-" + String.valueOf(userId);
    }

    /**
     * 校验关注Id
     * 关注Id要判断按横杆分割后是数字
     *
     * @param followId 关注Id
     * @return
     */
    public boolean verificationFollowId(String followId) {
        if (followId != null && !"".equals(followId.trim())) {
            String[] idGroup = followId.split("-");
            if (idGroup.length == 2) {
                boolean verification_1 = Verification.isPositiveIntegerZero(idGroup[0]);//数字
                boolean verification_2 = Verification.isPositiveIntegerZero(idGroup[1]);//数字
                if (verification_1 && verification_2) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 生成关注对象
     *
     * @return
     */
    public Object createFollowObject(Follow follow) {
        return follow;
    }

    /**
     * 异步拉取关注的用户消息
     *
     * @param userId   用户Id
     * @param userName 用户名称
     */
    @Async
    public void pullFollow(Long userId, String userName) {
        //查询我关注的用户
        List<Follow> followList = followManage.query_cache_findAllFollow(userId, userName);

        for (Follow follow : followList) {
            //标记Id
            String uuid = UUIDUtil.getUUID22();
            String flagId = followManage.query_cache_userUpdateFlag(follow.getFriendUserName(), uuid);
            if (flagId.equals(uuid)) {

                //最近话题发送时间
                Date topic_sendTime = follow.getAddtime();
                //最近评论发送时间
                Date comment_sendTime = follow.getAddtime();
                //最近回复发送时间
                Date reply_sendTime = follow.getAddtime();

                User user = userManage.query_cache_findUserByUserName(follow.getFriendUserName());
                if (user != null) {
                    //90.我关注的人发表了话题
                    Remind remind_90 = remindService.findNewRemindByUserId(userId, 90);
                    if (remind_90 != null) {
                        topic_sendTime = new Timestamp(remind_90.getSendTimeFormat());
                    }
                    //发送话题提醒
                    this.sendTopic(userId, userName, user.getId(), follow.getFriendUserName(), topic_sendTime);

                    //100.我关注的人发表了评论
                    Remind remind_100 = remindService.findNewRemindByUserId(userId, 100);
                    if (remind_100 != null) {
                        comment_sendTime = new Timestamp(remind_100.getSendTimeFormat());
                    }

                    //发送评论提醒
                    this.sendComment(userId, userName, user.getId(), follow.getFriendUserName(), comment_sendTime);

                    Remind remind_110 = remindService.findNewRemindByUserId(userId, 110);//110.我关注的人发表了回复
                    if (remind_110 != null) {
                        reply_sendTime = new Timestamp(remind_110.getSendTimeFormat());
                    }
                    //发送回复提醒
                    this.sendReply(userId, userName, user.getId(), follow.getFriendUserName(), reply_sendTime);
                }
            }
        }
    }

    /**
     * 发送话题提醒
     *
     * @param userId         用户Id
     * @param userName       用户名称
     * @param friendUserId   对方用户Id
     * @param friendUserName 对方用户名称
     * @param postTime       话题发表时间
     */
    private void sendTopic(Long userId, String userName, Long friendUserId, String friendUserName, Date postTime) {
        int page = 1;
        int maxresult = 200;// 每页显示记录数


        while (true) {
            //当前页
            int firstindex = (page - 1) * maxresult;
            //查询话题
            List<Topic> topicList = topicService.findTopicByPage(friendUserName, postTime, firstindex, maxresult);

            if (topicList == null || topicList.size() == 0) {
                break;
            }


            //提交提醒
            for (Topic topic : topicList) {
                Remind remind = new Remind();
                remind.setId(remindManage.createRemindId(userId));
                remind.setReceiverUserId(userId);//接收提醒用户Id
                remind.setSenderUserId(friendUserId);//发送用户Id
                remind.setTypeCode(90);//90.我关注的人发表了话题
                remind.setSendTimeFormat(topic.getPostTime().getTime());//发送时间格式化
                remind.setTopicId(topic.getId());//话题Id

                Object remind_object = remindManage.createRemindObject(remind);
                remindService.saveRemind(remind_object);

                //删除提醒缓存
                remindManage.delete_cache_findUnreadRemindByUserId(userId);
            }

            page++;
        }
    }

    /**
     * 发送评论提醒
     *
     * @param userId         用户Id
     * @param userName       用户名称
     * @param friendUserId   对方用户Id
     * @param friendUserName 对方用户名称
     * @param postTime       评论发表时间
     */
    private void sendComment(Long userId, String userName, Long friendUserId, String friendUserName, Date postTime) {
        int page = 1;
        int maxresult = 200;// 每页显示记录数

        while (true) {
            //当前页
            int firstindex = (page - 1) * maxresult;
            //查询话题
            List<Comment> commentList = commentService.findCommentByPage(friendUserName, postTime, firstindex, maxresult);
            if (commentList == null || commentList.size() == 0) {
                break;
            }


            //提交提醒
            for (Comment comment : commentList) {
                Remind remind = new Remind();
                remind.setId(remindManage.createRemindId(userId));
                remind.setReceiverUserId(userId);//接收提醒用户Id
                remind.setSenderUserId(friendUserId);//发送用户Id
                remind.setTypeCode(100);//100.我关注的人发表了评论
                remind.setSendTimeFormat(comment.getPostTime().getTime());//发送时间格式化
                remind.setTopicId(comment.getTopicId());//话题Id
                remind.setFriendTopicCommentId(comment.getId());//对方的话题评论Id

                Object remind_object = remindManage.createRemindObject(remind);
                remindService.saveRemind(remind_object);

                //删除提醒缓存
                remindManage.delete_cache_findUnreadRemindByUserId(userId);

            }

            page++;
        }
    }

    /**
     * 发送回复提醒
     *
     * @param userId         用户Id
     * @param userName       用户名称
     * @param friendUserId   对方用户Id
     * @param friendUserName 对方用户名称
     * @param postTime       评论发表时间
     */
    private void sendReply(Long userId, String userName, Long friendUserId, String friendUserName, Date postTime) {

        int page = 1;
        int maxresult = 200;// 每页显示记录数

        while (true) {
            //当前页
            int firstindex = (page - 1) * maxresult;
            //查询话题
            List<Reply> replyList = commentService.findReplyByPage(friendUserName, postTime, firstindex, maxresult);
            if (replyList == null || replyList.size() == 0) {
                break;
            }


            //提交提醒
            for (Reply reply : replyList) {
                Remind remind = new Remind();
                remind.setId(remindManage.createRemindId(userId));
                remind.setReceiverUserId(userId);//接收提醒用户Id
                remind.setSenderUserId(friendUserId);//发送用户Id
                remind.setTypeCode(110);//110.我关注的人发表了回复
                remind.setSendTimeFormat(reply.getPostTime().getTime());//发送时间格式化
                remind.setTopicId(reply.getTopicId());//话题Id


                remind.setFriendTopicCommentId(reply.getCommentId());//对方的话题评论Id
                remind.setFriendTopicReplyId(reply.getId());//对方的话题回复Id


                Object remind_object = remindManage.createRemindObject(remind);
                remindService.saveRemind(remind_object);

                //删除提醒缓存
                remindManage.delete_cache_findUnreadRemindByUserId(userId);

            }

            page++;
        }
    }

    /**
     * 查询缓存 查询关注
     *
     * @param followId 关注Id
     * @return
     */
    @Cacheable(value = "followManage_cache_findById", key = "#followId")
    public Follow query_cache_findById(String followId) {
        return followService.findById(followId);
    }

    /**
     * 删除缓存 关注
     *
     * @param followId 关注Id
     * @return
     */
    @CacheEvict(value = "followManage_cache_findById", key = "#followId")
    public void delete_cache_findById(String followId) {
    }


    /**
     * 查询缓存 我关注的用户的更新标记
     *
     * @param userName 用户名称
     * @param uuid     标记Id
     * @return
     */
    @Cacheable(value = "followManage_cache_userUpdateFlag", key = "#userName")
    public String query_cache_userUpdateFlag(String userName, String uuid) {
        return uuid;
    }

    /**
     * 删除缓存 我关注的用户的更新标记
     *
     * @param userName 用户名称
     * @return
     */
    @CacheEvict(value = "followManage_cache_userUpdateFlag", key = "#userName")
    public void delete_cache_userUpdateFlag(String userName) {
    }

    /**
     * 查询缓存 根据用户名称查询所有关注
     *
     * @param userId   用户Id
     * @param userName 用户名称
     * @return
     */
    @Cacheable(value = "followManage_cache_findAllFollow", key = "#userName")
    public List<Follow> query_cache_findAllFollow(Long userId, String userName) {
        return followService.findAllFollow(userId, userName);
    }

    /**
     * 删除缓存 根据用户名称查询所有关注
     *
     * @param userName 用户名称
     * @return
     */
    @CacheEvict(value = "followManage_cache_findAllFollow", key = "#userName")
    public void delete_cache_findAllFollow(String userName) {
    }

    /**
     * 查询缓存 查询关注总数
     *
     * @param userName 用户名称
     * @return
     */
    @Cacheable(value = "followManage_cache_followCount", key = "#userName")
    public Long query_cache_followCount(Long userId, String userName) {
        return followService.findFollowCountByUserName(userId, userName);
    }

    /**
     * 删除缓存 关注总数
     *
     * @param userName 用户名称
     * @return
     */
    @CacheEvict(value = "followManage_cache_followCount", key = "#userName")
    public void delete_cache_followCount(String userName) {
    }

}
